home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 18.8 KB | 680 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: TblPart.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "Table.hpp"
-
- // ----- TablePart Includes -----
-
- #ifndef TBLPART_H
- #include "TblPart.h"
- #endif
-
- #ifndef TBLFRAME_H
- #include "TblFrame.h"
- #endif
-
- #ifndef TBLPROXY_H
- #include "TblProxy.h"
- #endif
-
- #ifndef TBLSEL_H
- #include "TblSel.h"
- #endif
-
- #ifndef SOM_ODFExamples_ODFTableEmbeddedFramesIterator_xh
- #include "SOMIter.xh"
- #endif
-
- #ifndef TBLLINK_H
- #include "TblLink.h"
- #endif
-
- // ----- Part Layer -----
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWFCTCLP_H
- #include "FWFctClp.h"
- #endif
-
- #ifndef FWPRTITE_H
- #include "FWPrtIte.h"
- #endif
-
- #ifndef FWDRCMD_H
- #include "FWDrCmd.h"
- #endif
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWACQUIR_H
- #include "FWAcquir.h"
- #endif
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWMENU_H
- #include "FWMenu.h"
- #endif
-
- #ifndef FWPOINT_H
- #include "FWPoint.h"
- #endif
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- #ifndef FWSUSINK_H
- #include "FWSUSink.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- // ----- ODUtil -----
-
- #ifndef FWORDCOL_H
- #include "FWOrdCol.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWSTRING_H
- #include "FWString.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODArbitrator_xh
- #include <Arbitrat.xh>
- #endif
-
- #ifndef SOM_ODMenuBar_xh
- #include <MenuBar.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfTable
- #endif
-
- //========================================================================================
- // Globals
- //========================================================================================
-
- const ODValueType CTablePart::kPartKind = kODFTableKind;
- const ODValueType CTablePart::kPartUserName = kODFTableEditorUserString;
-
- //========================================================================================
- // class CTablePart
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CTablePart::CTablePart
- //----------------------------------------------------------------------------------------
-
- CTablePart::CTablePart(ODPart* odPart) :
- FW_CEmbeddingPart(odPart, CTablePart::kPartKind, CTablePart::kPartUserName, FW_gInstance, kPartIconID),
- fProxys(NULL),
- fTableLinkManager(NULL),
- fSubscribersRegistered(FALSE)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::~CTablePart
- //----------------------------------------------------------------------------------------
-
- CTablePart::~CTablePart()
- {
- if (fProxys)
- {
- CTableProxy* proxy;
- while ((proxy = (CTableProxy*)fProxys->First()) != NULL)
- {
- fProxys->Remove(proxy);
- delete proxy;
- }
- }
-
- delete fProxys;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::Initialize
- //----------------------------------------------------------------------------------------
-
- void CTablePart::Initialize(Environment* ev)
- {
- // ----- Call Inherited Initialize -----
- FW_CEmbeddingPart::Initialize(ev);
-
- // ----- Register Presentation -----
- fTablePresentation = RegisterPresentation(ev, "Apple:Presentation:ODFTable", TRUE, new CTableSelection(ev, this));
-
- // ----- Create list of proxies -----
- fProxys = new FW_CPrivOrderedCollection;
-
- // ----- Build Table menu -----
- InitializeMenuBar(ev, this->GetMenuBar(ev));
-
- fCells.fX = kMaxCols;
- fCells.fY = kMaxRows;
-
- // ----- Initialize arrays
- unsigned long s;
- for(s=0; s < kMaxCols; s++)
- fWidth[s] = kDefaultCellWidth;
- for(s=0; s < kMaxRows; s++)
- fHeight[s] = kDefaultCellHeight;
- }
-
- //------------------------------------------------------------------------------
- // CTablePart::InitializeMenuBar
- //------------------------------------------------------------------------------
-
- void CTablePart::InitializeMenuBar(Environment *ev, FW_CMenuBar *menuBar)
- {
- FW_CSharedLibraryResourceFile resFile; // Open the resource file of this shared library
-
- #ifdef FW_BUILD_WIN
- // [HLX] Temporary embed menu for windows
- WinAddEmbedMenu(ev, menuBar);
- #endif
-
- FW_CPullDownMenu* tableMenu = new FW_CPullDownMenu(ev, resFile, kMenuStrings, kTableStr);
- tableMenu->AppendToggleItem(ev, resFile, kMenuStrings, kHideGridStr, kShowGridStr, cHideShowGrid);
- menuBar->AdoptMenuLast(ev, tableMenu);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::ExternalizeContent
- //----------------------------------------------------------------------------------------
-
- void CTablePart::ExternalizeContent(Environment *ev, ODStorageUnit* su, FW_CCloneInfo* cloneInfo)
- {
- ODValueType propertyValueType = this->GetPartKind(ev);
- su->Focus(ev, kODPropContents, kODPosUndefined, propertyValueType, 0, kODPosUndefined);
- su->Remove(ev);
- su->AddValue(ev, propertyValueType);
-
- // ---- Write out grid info ----
- FW_CStorageUnitSink sink(su, kODPropContents, propertyValueType);
- FW_CWritableStream archive(&sink);
-
- archive << fCells.fX;
- archive << fCells.fY;
- archive.Write(fWidth, fCells.fX);
- archive.Write(fHeight, fCells.fY);
-
- // ---- Write number of embedded parts ----
- unsigned long partCount = fProxys->Count();
- archive << partCount;
-
- // ---- Write out embedded parts ----
- if (partCount > 0)
- {
- FW_COrderedCollectionIterator iter(fProxys);
- for (CTableProxy* proxy = (CTableProxy*)iter.First(); iter.IsNotComplete(); proxy = (CTableProxy*)iter.Next())
- {
- CCell cell = proxy->GetCell();
- archive << cell.fX;
- archive << cell.fY;
- proxy->Externalize(ev, sink.GetStorageUnitView(), cloneInfo);
- }
- }
-
- //--- Write source and destination links, if any ---
- fTableLinkManager->ExternalizeLinks(ev, sink.GetStorageUnitView(), cloneInfo);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::InternalizeContent
- //----------------------------------------------------------------------------------------
-
- void CTablePart::InternalizeContent(Environment* ev, ODStorageUnit* su, FW_CCloneInfo* cloneInfo)
- {
- // ---- Read grid info -----
- FW_CStorageUnitSink sink(su, kODPropContents, this->GetPartKind(ev));
- FW_CReadableStream archive(&sink);
-
- archive >> fCells.fX;
- archive >> fCells.fY;
-
- FW_ASSERT(fCells.fX == kMaxCols);
- archive.Read(fWidth, fCells.fX);
-
- FW_ASSERT(fCells.fY == kMaxRows);
- archive.Read(fHeight, fCells.fY);
-
- // ---- Read number of embedded parts ----
- unsigned long partCount;
- archive >> partCount;
-
- // ---- Read embedded parts ----
- CCell cell;
- CTableProxy* proxy;
- for (unsigned long i = 0; i < partCount; i++)
- {
- archive >> cell.fX;
- archive >> cell.fY;
- proxy = new CTableProxy(ev, this, fTablePresentation);
- proxy->SetCell(cell);
- proxy->Internalize(ev, sink.GetStorageUnitView(), cloneInfo); // read part and embed it
- this->AddProxy(proxy);
- }
-
- // ----- Read link information -----
- fTableLinkManager->InternalizeLinks(ev, su);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::NewFrame
- //----------------------------------------------------------------------------------------
-
- FW_CFrame* CTablePart::NewFrame(Environment* ev,
- ODFrame* odFrame,
- FW_CPresentation* presentation,
- FW_Boolean fromStorage)
- {
- FW_UNUSED(fromStorage);
-
- return new CTableFrame(ev, odFrame, presentation, this);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::GetWidth
- //----------------------------------------------------------------------------------------
-
- FW_CFixed CTablePart::GetWidth(short c) const
- {
- return (c < fCells.fX ? fWidth[c] : kDefaultCellWidth);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::GetHeight
- //----------------------------------------------------------------------------------------
-
- FW_CFixed CTablePart::GetHeight(short r) const
- {
- return (r < fCells.fY ? fHeight[r] : kDefaultCellHeight);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::SetWidth
- //----------------------------------------------------------------------------------------
-
- void CTablePart::SetWidth(short c, FW_CFixed w)
- {
- FW_ASSERT(c < fCells.fX);
- fWidth[c] = w;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::SetHeight
- //----------------------------------------------------------------------------------------
-
- void CTablePart::SetHeight(short r, FW_CFixed h)
- {
- FW_ASSERT(r < fCells.fY);
- fHeight[r] = h;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::HitTest
- //----------------------------------------------------------------------------------------
-
- ETableLoc CTablePart::HitTest(Environment* ev,
- const CTableFrame* frame,
- const FW_CPoint& where,
- CCell& cell) const
- {
- FW_ASSERT(frame != NULL);
-
- // Verify point is in table area (also exclude the top or left margins)
- FW_CRect rect;
- frame->GetRect(ev, rect);
-
- cell.fX = cell.fY = 0;
-
- if(!rect.Contains(where))
- return kTLNone;
-
- FW_CRect cellRect;
- while (TRUE)
- {
- FindRect(cell, cellRect);
- cellRect.Inset(-kPenWidth, -kPenHeight);
- FW_CPoint testPoint(where.x, cellRect.top);
- if (cellRect.Contains(testPoint))
- break;
- cell.fX += 1;
- }
-
- while (TRUE)
- {
- FindRect(cell, cellRect);
- cellRect.Inset(-kPenWidth, -kPenHeight);
- FW_CPoint testPoint(cellRect.left, where.y);
- if (cellRect.Contains(testPoint))
- break;
- cell.fY += 1;
- }
-
- ETableLoc tl = kTLCell;
-
- FindRect(cell, cellRect);
-
- if (where.x <= cellRect.left + kHMargin)
- tl += kTLLeftBorder;
- else if (cellRect.right - kHMargin <= where.x)
- tl += kTLRightBorder;
-
- if (where.y <= cellRect.top + kVMargin)
- tl += kTLTopBorder;
- else if (cellRect.bottom - kVMargin <= where.y)
- tl += kTLBottomBorder;
-
- if (cell.fY == 0 && ((tl & kTLTopBorder) != 0))
- tl = kTLNone;
-
- if (cell.fX == 0 && ((tl & kTLLeftBorder) != 0))
- tl = kTLNone;
-
- return tl;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::FindLeft
- //----------------------------------------------------------------------------------------
-
- FW_CFixed CTablePart::FindLeft(short col) const
- {
- FW_CFixed x = kHMargin + kBorderWidth;
- for(short c = 0; c < col; c += 1)
- x += this->GetWidth(c) + kBorderWidth;
- return x;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::FindTop
- //----------------------------------------------------------------------------------------
-
- FW_CFixed CTablePart::FindTop(short row) const
- {
- FW_CFixed y = kVMargin + kBorderHeight;
- for(short r = 0; r < row; r += 1)
- y += this->GetHeight(r) + kBorderWidth;
- return y;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::FindRect
- //----------------------------------------------------------------------------------------
-
- void CTablePart::FindRect(const CCell& cell,
- FW_CRect& rect) const
- {
- FW_CFixed x = this->FindLeft(cell.fX);
- FW_CFixed y = this->FindTop (cell.fY);
-
- rect.Set(x, y,
- x + this->GetWidth(cell.fX),
- y + this->GetHeight(cell.fY));
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::CellToProxy
- //----------------------------------------------------------------------------------------
-
- CTableProxy* CTablePart::CellToProxy(Environment* ev, const CCell& cell) const
- {
- FW_COrderedCollectionIterator iter(fProxys);
- for (CTableProxy* proxy = (CTableProxy*)iter.First(); iter.IsNotComplete(); proxy = (CTableProxy*)iter.Next())
- {
- if (proxy->GetCell() == cell)
- return proxy;
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::Resize
- //----------------------------------------------------------------------------------------
-
- void CTablePart::Resize(Environment* ev, const CCell& cell, ETableLoc tl, const FW_CPoint& delta)
- {
- CCell topLeftCell(cell);
-
- if (delta.x)
- {
- if ((tl & kTLLeftBorder) != 0)
- topLeftCell.fX -= 1;
- this->SetWidth(topLeftCell.fX, this->GetWidth (topLeftCell.fX) + delta.x);
- }
-
- if (delta.y)
- {
- if ((tl & kTLTopBorder) != 0)
- topLeftCell.fY -= 1;
- this->SetHeight(topLeftCell.fY, this->GetHeight(topLeftCell.fY) + delta.y);
- }
-
- // Move all embedded frames
- FW_COrderedCollectionIterator ite(fProxys);
- for (CTableProxy* proxy = (CTableProxy*)ite.First(); ite.IsNotComplete(); proxy = (CTableProxy*)ite.Next())
- {
- // Get operations being performed
- CCell frCell = proxy->GetCell();
- FW_Boolean resizing = (frCell.fX == topLeftCell.fX || frCell.fY == topLeftCell.fY);
- FW_Boolean moving = (frCell.fX > topLeftCell.fX || frCell.fY > topLeftCell.fY);
-
- if (resizing || moving)
- proxy->MoveEmbeddedFrames(ev, frCell);
- }
-
- this->Changed(ev);
-
- fTablePresentation->Invalidate(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::AddProxy
- //----------------------------------------------------------------------------------------
-
- void CTablePart::AddProxy(CTableProxy* proxy)
- {
- fProxys->AddLast(proxy);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::RemoveProxy
- //----------------------------------------------------------------------------------------
-
- void CTablePart::RemoveProxy(CTableProxy* proxy)
- {
- fProxys->Remove(proxy);
- }
-
- //========================================================
- // ADDITIONS FOR LINKING SUPPORT
- //========================================================
-
- //----------------------------------------------------------------------------------------
- // CTablePart::ContentUpdated
- //----------------------------------------------------------------------------------------
- void CTablePart::ContentUpdated(Environment* ev, FW_CFrame* frame, ODUpdateID change)
- {
- FW_CEmbeddingPart::ContentUpdated(ev, frame, change);
-
- //--- Update affected link sources ---
- if (change == kODUnknownUpdate)
- fTableLinkManager->UpdateLinkSource(ev, change);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::RegisterSubscribers
- //----------------------------------------------------------------------------------------
-
- void CTablePart::RegisterSubscribers(Environment* ev)
- {
- //--- Check for subscribers that need to be registered for automatic updates
- if (!fSubscribersRegistered) // only want to do this once!
- {
- fTableLinkManager->RegisterSubscribers(ev);
- fSubscribersRegistered = TRUE;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::ShowLinkInfo
- //----------------------------------------------------------------------------------------
- FW_Boolean CTablePart::ShowLinkInfo(Environment* ev, FW_CFrame* frame)
- {
- return fTableLinkManager->ShowLinkInfo(ev, frame->GetActiveFacet(ev));
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::IsLinkSelected
- //----------------------------------------------------------------------------------------
- FW_Boolean CTablePart::IsLinkSelected(Environment* ev)
- {
- /* for now, return TRUE if a link's cell is selected */
- if (fTableLinkManager->GetSelectedLinkSource(ev) != NULL)
- return TRUE;
- else if (fTableLinkManager->GetSelectedLinkDest(ev) != NULL)
- return TRUE;
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::ChangeFrameStatus
- //----------------------------------------------------------------------------------------
-
- void CTablePart::ChangeFrameStatus(Environment* ev, const CCell& cell, ODLinkStatus newStatus)
- {
- CTableProxy* proxy = this->CellToProxy(ev, cell);
- FW_ASSERT(proxy);
-
- proxy->ChangeLinkStatus(ev, newStatus);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::AboutToClearCell
- //----------------------------------------------------------------------------------------
- void CTablePart::AboutToClearCell(Environment* ev, const CCell& cell)
- {
- //-- Break affected links --
- fTableLinkManager->BreakExistingLinks(ev, cell);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::ProxyMoved
- //----------------------------------------------------------------------------------------
- void CTablePart::ProxyMoved(Environment* ev,
- const CCell& fromCell,
- const CCell& toCell)
- {
- //-- Adjust affected links --
- fTableLinkManager->MoveExistingLinks(ev, fromCell, toCell);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::GetTableSelection
- //----------------------------------------------------------------------------------------
- CTableSelection* CTablePart::GetTableSelection(Environment* ev)
- {
- return (CTableSelection*) fTablePresentation->GetSelection(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::DoAdjustMenus
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CTablePart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
- {
- if (hasMenuFocus)
- {
- //--- Set up the Part Info menu item ---
- if (this->IsLinkSelected(ev))
- {
- menuBar->SetItemString(ev, kODCommandGetPartInfo, FW_CString32("Link Info"));
- menuBar->EnableCommand(ev, kODCommandGetPartInfo, TRUE);
- }
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // CTablePart::NewLinkManager
- //----------------------------------------------------------------------------------------
-
- FW_CLinkManager* CTablePart::NewLinkManager(Environment *ev) // Override
- {
- CTableLinkManager* linkMgr = FW_NEW(CTableLinkManager, (ev, this));
- fTableLinkManager = linkMgr;
- return linkMgr;
- }
-